<template>
  <div class="charts" ref="chartsRef"></div>
</template>

<script lang="ts">
export default {
  name: "SalesChart",
};
</script>

<script lang="ts" setup>
import * as echarts from "echarts";
import { ref, watch } from "vue";
// setup相当于beforecreate和created阶段
// 要挂载才有dom元素
const chartsRef = ref();
const props = defineProps<{
  data: number[];
  axis: string[];
  title: string;
}>();
watch(
  () => props.data,
  () => {
    const myEcharts = echarts.init(chartsRef.value);
    const option = {
      title: {
        text: props.title,
      },
      xAxis: {
        show: true,
        data: props.axis,
      },
      yAxis: {},
      grid: {
        left: 40,
        right: 40,
        bottom: 20,
      },
      // 提示组件，鼠标移入有数据提示
      tooltip: {},
      series: [
        {
          type: "bar",
          data: props.data,
          itemStyle: {
            color: "#3398db",
          },
          barWidth: 25,
        },
      ],
    };
    myEcharts.setOption(option);
  }
);
</script>

<style scoped>
.charts {
  width: 100%;
  height: 300px;
}
</style>
